package com.example.saveword.save_word;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.saveword.R;
import com.example.saveword.dao.StudyRecordDao;
import com.example.saveword.dao.WordDao;
import com.example.saveword.dao.WordRecordDao;
import com.example.saveword.model.User;
import com.example.saveword.model.Word;
import com.example.saveword.utils.JsonUtils;
import com.example.saveword.utils.SPUtils;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WordBookFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WordBookFragment extends Fragment implements View.OnClickListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    protected View view; // 声明一个视图对象
    protected Context context; // 上下文对象

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    // 用户信息
    private User user;
    private int id;
    // 单词本
    private View cet4, cet6, my;
    private TextView tv_wordBook_title_cet4, tv_wordBook_title_cet6, tv_wordBook_title_my;
    private TextView tv_progress_cet4, tv_progress_cet6, tv_progress_my;
    private int cet4WordAmount, cet6WordAmount, myWordAmount;
    private int cet4WordLearningAmount, cet6WordLearningAmount;
    private WordDao wordDao;
    private StudyRecordDao studyRecordDao;
    // 进度条
    private ProgressBar progressBar_cet4, progressBar_cet6;

    private Button bt_add;

    public WordBookFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ReviewFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static WordBookFragment newInstance(String param1, String param2) {
        WordBookFragment fragment = new WordBookFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity(); // 获取活动页面的上下文
        view = inflater.inflate(R.layout.fragment_word_book, container, false);
        init();
        getData();
        // Inflate the layout for this fragment
        return view;
    }

    public void init() {
        wordDao = new WordDao(context);
        studyRecordDao = new StudyRecordDao(context);

        cet4 = view.findViewById(R.id.icl_cet4);
        tv_wordBook_title_cet4 = cet4.findViewById(R.id.tv_wordBook_title);
        tv_progress_cet4 = cet4.findViewById(R.id.tv_progress);
        progressBar_cet4 = cet4.findViewById(R.id.pbMedal);

        cet6 = view.findViewById(R.id.icl_cet6);
        tv_wordBook_title_cet6 = cet6.findViewById(R.id.tv_wordBook_title);
        tv_progress_cet6 = cet6.findViewById(R.id.tv_progress);
        progressBar_cet6 = cet6.findViewById(R.id.pbMedal);

        my = view.findViewById(R.id.icl_my);
        tv_wordBook_title_my = my.findViewById(R.id.tv_wordBook_title);
        tv_progress_my = my.findViewById(R.id.tv_progress);
        bt_add = my.findViewById(R.id.addWord);
        bt_add.setOnClickListener(this);
    }

    public void getData() {
        getUserData();
        setCet4();
        setCet6();
        setMy();
    }

    public void getUserData() {
        user = (User) JsonUtils.jsonToObject((String) SPUtils.get(context, "User", ""), User.class);
        id = user.get_id();
    }

    public void setCet4() {
        cet4.setOnClickListener(this);
        tv_wordBook_title_cet4.setText("四级词汇大全");
        cet4WordAmount = wordDao.getTypeCount("CET4");
        cet4WordLearningAmount = studyRecordDao.getLearningWordByIdAndType(String.valueOf(id), "CET4");
        String progress = cet4WordLearningAmount + "/" + cet4WordAmount;
        tv_progress_cet4.setText(progress);
        progressBar_cet4.setMax(cet4WordAmount);
        progressBar_cet4.setProgress(cet4WordLearningAmount);
    }

    public void setCet6() {
        cet6.setOnClickListener(this);
        tv_wordBook_title_cet6.setText("六级词汇大全");
        cet6WordAmount = wordDao.getTypeCount("CET6");
        cet6WordLearningAmount = studyRecordDao.getLearningWordByIdAndType(String.valueOf(id), "CET6");
        String progress = cet6WordLearningAmount + "/" + cet6WordAmount;
        tv_progress_cet6.setText(progress);
        progressBar_cet6.setMax(cet6WordAmount);
        progressBar_cet6.setProgress(cet6WordLearningAmount);
    }

    public void setMy() {
        my.setOnClickListener(this);
        tv_wordBook_title_my.setText("我的单词本");
        myWordAmount = wordDao.getTypeCount("my");
        String progress = String.valueOf(myWordAmount);
        tv_progress_my.setText(progress);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.icl_cet4) {
            Intent intent = new Intent();
            intent.setClass(context, WordListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("wordListType", "CET4");
            intent.putExtras(bundle);
            startActivity(intent);
        } else if(view.getId() == R.id.icl_cet6) {
            Intent intent = new Intent();
            intent.setClass(context, WordListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("wordListType", "CET6");
            intent.putExtras(bundle);
            startActivity(intent);
        } else if(view.getId() == R.id.icl_my) {
            Intent intent = new Intent();
            intent.setClass(context, WordListActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("wordListType", "my");
            intent.putExtras(bundle);
            startActivity(intent);
        } else if(view.getId() == R.id.addWord) {
            showAddDialog();
        }
    }

    protected void showAddDialog() {
        LayoutInflater factory = LayoutInflater.from(context);
        final View textEntryView = factory.inflate(R.layout.addword, null);
        final EditText editTextWord = (EditText) textEntryView
                .findViewById(R.id.add_word);
        final EditText editTextMean = (EditText) textEntryView
                .findViewById(R.id.add_mean);
        final EditText editTextVoice = (EditText) textEntryView
                .findViewById(R.id.add_voice);
        final EditText editTextExp = (EditText) textEntryView
                .findViewById(R.id.add_exp);
        final EditText En_mean = (EditText) textEntryView
                .findViewById(R.id.add_En_mean);
        final EditText add_WordGroup = (EditText) textEntryView
                .findViewById(R.id.add_WordGroup);
        final EditText add_near = (EditText) textEntryView
                .findViewById(R.id.add_near);
        AlertDialog.Builder ad1 = new AlertDialog.Builder(context);
        ad1.setTitle("添加单词信息:");
        ad1.setIcon(R.drawable.ic___);
        ad1.setView(textEntryView);
        ad1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
                Log.i("111111", editTextWord.getText().toString());
                Word word = new Word();
                word.setHeadWord(editTextWord.getText().toString());
                word.setTranCN(editTextMean.getText().toString());
                word.setSentences(editTextExp.getText().toString());
                word.setUkphone(editTextVoice.getText().toString());
                word.setTranEN(En_mean.getText().toString());
                word.setPhrases(add_WordGroup.getText().toString());
                word.setSyno(add_near.getText().toString());
                word.setWordType("my");
                wordDao.add(word);
                setMy();
            }
        });
        ad1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int i) {
            }
        });
        ad1.show();// 显示对话框
    }
    @Override
    public void onResume() {
        super.onResume();
        setCet4();
        setCet6();
        setMy();
    }
}